package br.ufal.ic.sd.testes.segundaEntrega;

import java.util.Observable;
import java.util.Observer;
import br.ufal.ic.sd.serverSide.serverOptions.Main;
import br.ufal.ic.sd.serverSide.servicos.ServicoTokenRing;

public class TesteTokenRing implements Observer{
	
	
	 String[] array0 = {"9000", "9001", "config.xml","teste.txt", " ","true"  };
	 String[] array1 = {"9001", "9002", "config.xml", "teste2.txt", "tokenringstart", "true" };
	 String[] array2 = {"9002", "9000", "config.xml", "teste3.txt", " " ,"true" };
     String[] arrayServer = {"9000", "9001", "9002"};
     int contador = 1;
     
	public TesteTokenRing(){
	 
     Thread t0 = new Thread(new Runnable() {
         public void run() {
             Main a0 = new Main();
             a0.main(array0);
         }
     });
     t0.start();
     
     Thread t1 = new Thread(new Runnable() {
         public void run() {
             Main a1 = new Main();
             a1.main(array1);
         }
     });
     t1.start();
     
     Thread t2 = new Thread(new Runnable() {
         public void run() {
             Main a2 = new Main();
             a2.main(array2);
         }
     });
     t2.start();
    
     
     //add observers
     ServicoTokenRing s1 = ServicoTokenRing.getInstance(9001, 9000);
     ServicoTokenRing s2 = ServicoTokenRing.getInstance(9002, 9001);
     ServicoTokenRing s3 = ServicoTokenRing.getInstance(9000, 9002);
     
     s1.addObserver(this);
     s2.addObserver(this);
     s3.addObserver(this);
}	

	@Override
	public void update(Observable o, Object arg) {
		
		String aux = arg.toString();
		
		if(aux.equals(arrayServer[contador])){
			System.out.println("token chegou no server de porta " + arg);
		}else{
			System.out.println("token na porta errada"+arg);
		}
		
		if(contador == 2){
			contador = 0;
		}
		else{
			contador++;	
		}
	}
}
 